﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using Yedda;
using lib;
using lib.Twitter;
using lib.JsonParser;


namespace myTwitter
{
    public class myTwitter
    {
        public event EventHandler newStatus;

        private String userName, password;
        private Twitter twitter;

        public myTwitter(String userName, String password)
        {
            twitter = new Twitter();
            twitter.TwitterClientVersion = "Posted from One4All";
            this.userName = userName;
            this.password = password;
        }

        // Invoke the newStatus event when ever a new status is recieved
        protected virtual void OnNewStatus(EventArgs e)
        {
            if (newStatus != null)
                newStatus(this, e);
        }

        public bool authUser()
        {
            try
            {
                String json = getUserStatus();
                if (json == null)
                    return false;
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public String getUserStatus(String usrScreenName)
        {
            String json;

            if(usrScreenName == null)
                json = twitter.GetUserTimelineAsJSON(this.userName, this.password);
            else
                json = twitter.GetUserTimelineAsJSON(this.userName, this.password, usrScreenName);
            return json;
        }

        public String getUserStatus()
        {
            return getUserStatus(null);
        }
       
        public String getFriendStatus()
        {
            return twitter.GetFriendsTimelineAsJSON(this.userName, this.password);
        }

        public String getPublicTimeline()
        {
            return twitter.GetPublicTimelineAsJSON();
        }

        public String getFriends()
        {
            return twitter.GetFriendsAsJSON(this.userName, this.password);
        }

        public String updateStatus(String status)
        {
            try
            {
                return twitter.UpdateAsJSON(this.userName, this.password, status);
            }
            catch (Exception e)
            {
                Debug.WriteLine("UpdateStatusException: " + e.Message);
                return "";
            }
        }

        public bool updateDB(User myUser)
        {
            try
            {
                String json = this.getFriendStatus();
                // Parse json string to json object format
                Object jsonObject = JsonParser.parseJson(json);
                // Parse json object format to list of object for the databse tables
                List<Object> olist = JsonTwitter.jsonToTwitter(jsonObject, "Status");
                if (lib.Twitter.TwitterLinq.insertTwitter(olist,myUser))
                {
                    // New status recieved.
                    // Invoke a new status event!
                    OnNewStatus(EventArgs.Empty);
                }
                return true;
            }
            catch (Exception e)
            {
                Debug.WriteLine("Exception in " + e.TargetSite + ": " + e.Message);
                return false;
            }
        }
    }
}
